﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MP3Ganizer.BusinessObject.Hierarchy
{
    public class FolderModel : IHierarchy, MP3Ganizer.BusinessObject.Hierarchy.IFolderModel
    {
        public FolderModel(string label, IEnumerable<IHierarchy> children, HierarchyItemType type)
        {
            Label = label;
            Children = children;
            Type = type;
        }

        public string Label { get; private set; }
        public HierarchyItemType Type { get; private set; }
        public IEnumerable<IHierarchy> Children { get; private set; }

        public IEnumerable<IFileModel> FileChildren
        {
            get 
            {
                return Children.SelectMany(c => GetFiles(c));
            }
        }

        private IEnumerable<IFileModel> GetFiles(IHierarchy c)
        {
            if(c is IFolderModel)
            {
                var folder = c as IFolderModel;
                return folder.Children.SelectMany(f => GetFiles(f));
            }
            else if(c is IFileModel)
            {
                var file = c as IFileModel;
                return new IFileModel[1]{file}.AsEnumerable();
            }
            return null;
        }
    }
}
